<?php
/**
 *
 * Author: luffyzhao@vip.126.com
 * DateTime: 2019/3/26 10:44
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DistributionUser extends Authenticatable
{
    protected $fillable = ['role_id', 'name', 'phone', 'email', 'password', 'status', 'remark'];

    protected $hidden = [
        'password'
    ];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('sort', function (Builder $builder){
            if (request()->has('distribution_id')) {
                $builder->where('distribution_id', '=', request()->input('distribution_id'));
            }
        });
    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/3/28 19:51
     */
    public function role()
    {
        return $this->belongsTo(DistributionRole::class, 'role_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function logs(){
        return $this->hasMany(DistributionLog::class, 'user_id');
    }

    /**
     * @param $value
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/3/29 19:48
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }

    /**
     * 允许登录
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function allowLogin(){
        return $this->getAttribute('status') === 1;
    }

}
